#include"inputbufferstream.h"
#include<errno.h>
#include<fcntl.h>
#include<iostream>
#include<cstring>
#include<cstdio>

void inputbufferstream::open_s(const char *fileName)
{
	this->fd = open(fileName, O_RDONLY);
	if(this->fd == -1)
                std::cout << "Error opening stream: " << strerror( errno ) << std::endl;
	else
		readBuf = new int[BUFFER];
}

bool inputbufferstream::end_of_stream()
{
        if(this->readBuf[this->rcount] == EOF)
                return true;
       	return false;
}

int inputbufferstream::read_next()
{
	if(this->rcount==0 || this->rcount == BUFFER)
	{
		if((read(this->fd, readBuf, BUFFER*sizeof(int))) < 0)
			 std::cout << "Error reading from stream: " << strerror( errno ) << std::endl;
		this->rcount = 0;
	}
	if(!end_of_stream())
		return this->readBuf[this->rcount++];
	else
		return EOF;
}

void inputbufferstream::close_i()
{
	close(this->fd);
}
